<?php
/**
 * Date: 8/4/11
 * Time: 7:59 PM
 */
class SmileModel extends CI_Model{
    var $_id;
    var $_title;
    var $_text;
    var $_path;
    private $_table = 'smile';
    
    function __construct(){
        parent::__construct();
    }

    /**
     * @return smile array
     */
    function getSmileList(){
        $this->db->cache_on();
        $query = $this->db->get($this->_table);
        return $query->result_array();
    }

    function getTextList(){
        $sql = 'select text from smile';
        $this->db->cache_on();
        $query = $this->db->query($sql);
        $temp =  $query->result_array();
        $result = array();
        foreach($temp as $item)
            $result[] = $item['text'];
        return $result;
    }

    function getPathList(){
        $sql = 'select path from smile';
        $this->db->cache_on();
        $query = $this->db->query($sql);
        $temp =  $query->result_array();
        $result = array();
        foreach($temp as $item)
            $result[] = $item['path'];
        return $result;
    }
    function insert(){
        $data = array(
            'title' => $this->_title,
            'text' => $this->_text,
            'path' => $this->_path
        );
        $this->db->insert($this->_table, $data);
        return $this->db->insert_id();
    }

    function getSmile($smileID){
        $this->db->cache_on();
        $query = $this->db->get_where($this->_table, array('id'=>$smileID), 1, 0);
        return $query->row_array();
    }
    
    function update(){
        if($this->_path == '')
            $data = array(
                'title' => $this->_title,
                'text' => $this->_text,
            );
        else
            $data = array(
                'title' => $this->_title,
                'text' => $this->_text,
                'path' => $this->_path
            );
        $this->db->where('id', $this->_id);
        $this->db->update($this->_table, $data);
    }
}

